Namespace Actions.ActionItems
    <ActionItemWordAttribute("filetimestamps")> _
    Public Class aiFileTimeStamps
        Inherits ActionItem
        Public RsName As String = "default"
        Public DirectoryName As String = "d:\"
        Public Delimiter As String = ";"

        Public FileNameLength As Integer = 255

        Private rs As New ADODB.Recordset

        Public Sub New(ByVal Owner As Action, ByVal Definition As Xml.XmlNode)
            MyBase.New(Owner, Definition)
            rs.Fields.Append("FileName", ADODB.DataTypeEnum.adVarChar, FileNameLength)
            rs.Fields.Append("FileDate", ADODB.DataTypeEnum.adDBDate)
            rs.Fields.Append("FileTime", ADODB.DataTypeEnum.adDBTime)
            rs.Open()
        End Sub

        Public Overrides Sub Execute()
            RsName = Configuration.Attribute("name", "default")
            DirectoryName = Configuration.Attribute("directory", "")

            DirectoryName = OwnerAction.InsertParams(DirectoryName)
            Dim files() As String = IO.Directory.GetFiles(DirectoryName)

            OwnerAction.Resources.Recordsets(RsName) = rs

            Dim FileName As String
            Dim FileDate As Date

            Dim ShouldWriteLine As Boolean = False
            For Each FullFileName As String In files
                FileName = IO.Path.GetFileNameWithoutExtension(FullFileName)
                FileDate = IO.File.GetLastWriteTime(FullFileName)

                If FileName.Length > FileNameLength Then
                    FileName = FileName.Substring(0, FileNameLength)
                End If

                rs.AddNew()
                rs("FileName").Value = FileName
                rs("FileDate").Value = FileDate.ToString("yyyy-MM-dd")
                rs("FileTime").Value = FileDate.ToString("hh:mm:ss")
                rs.Update()
                Threading.Thread.Sleep(1)
            Next
        End Sub
    End Class
End Namespace